package com.coollang.smartbra.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.utils.DisplayUtils;


public class BaseLineView extends RelativeLayout {

    private ImageView imgLeft;
    private TextView tvLeft;
    private TextView tvRight;
    private ImageView imgRight;

    private int IMAGE_SIZE_DP = 24;
    private int VIEW_HEIGHT_DP = 48;
    private int TEXT_SIZE_SP = 14;
    private int BASE_MARGIN_DP = 8;
    private int HOR_PADDING = 16;

    /**
     * drawable控制左右图片内容，visible控制是否可见； 如果leftDrawable == null || leftVisible ==
     * false ; 则左侧图片不可见且左侧文字顶上，没有margin参数； 如果leftDrawable != null && leftVisible
     * == ture ; 则左侧图片可见且左侧文字在图片右边 ， 有margin参数；
     * 考虑到多个View一列列展示时，有对齐因素，所以只有当rightVisible == false 时 ，
     * 右侧图片不可见且右侧文字没有margin参数； 否则右侧图片显示，右侧文字有margin参数
     */
    private Drawable leftDrawable, rightDrawable;
    private boolean leftVisible, rightVisible;

    private String leftText, rightText;
    private ColorStateList leftColor, rightColor;
    private int leftSize, rightSize, hor_padding;

    public BaseLineView(Context context) {
        this(context, null);
    }

    public BaseLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public BaseLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BaseLineView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr);
    }

    /**
     * drawable 可以为null
     *
     * @param context      上下文
     * @param attrs        View参数
     * @param defStyleAttr 默认参数
     */
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.baseLineView, defStyleAttr, 0);

        leftVisible = typedArray.getBoolean(R.styleable.baseLineView_leftImageVisible, true);
        rightVisible = typedArray.getBoolean(R.styleable.baseLineView_rightImageVisible, true);
        leftDrawable = typedArray.getDrawable(R.styleable.baseLineView_leftDrawable);
        rightDrawable = typedArray.getDrawable(R.styleable.baseLineView_rightDrawable);

        leftText = typedArray.getString(R.styleable.baseLineView_leftText);
        rightText = typedArray.getString(R.styleable.baseLineView_rightText);
        leftColor = typedArray.getColorStateList(R.styleable.baseLineView_leftTextColor);
        rightColor = typedArray.getColorStateList(R.styleable.baseLineView_rightTextColor);
        leftSize = DisplayUtils.px2dip(context, typedArray.getDimensionPixelSize(R.styleable.baseLineView_leftTextSize, 0));
        rightSize = DisplayUtils.px2dip(context, typedArray.getDimensionPixelSize(R.styleable.baseLineView_rightTextSize, 0));
        hor_padding = typedArray.getDimensionPixelSize(R.styleable.baseLineView_horizontalPadding, 0);

        if (leftSize == 0) {
            leftSize = TEXT_SIZE_SP;
        }
        if (rightSize == 0) {
            rightSize = TEXT_SIZE_SP;
        }
        if (hor_padding == 0) {
            hor_padding = DisplayUtils.dip2px(context, HOR_PADDING);
        }

        typedArray.recycle();

        if (null == leftText) {
            leftText = "";
        }
        if (null == rightText) {
            rightText = "";
        }
        if (null == leftColor) {
            leftColor = ColorStateList.valueOf(Color.BLACK);
        }
        if (null == rightColor) {
            rightColor = ColorStateList.valueOf(Color.BLACK);
        }

        int horizontal = hor_padding;
        int vertical = DisplayUtils.dip2px(context, BASE_MARGIN_DP);
        setPadding(horizontal, vertical, horizontal, vertical);

        initLeftImage();
        initRightImage();

        initLeftText();
        initRightText();

        addView(imgLeft);
        addView(imgRight);
        addView(tvLeft);
        addView(tvRight);
    }

    private void initLeftImage() {
        LayoutParams imgLeftParams = new LayoutParams(DisplayUtils.dip2px(getContext(), IMAGE_SIZE_DP), DisplayUtils.dip2px(getContext(),
                IMAGE_SIZE_DP));
        imgLeftParams.addRule(RelativeLayout.CENTER_VERTICAL);
        imgLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        imgLeft = new ImageView(getContext());
        imgLeft.setLayoutParams(imgLeftParams);
        imgLeft.setId(R.id.baseline_left_img);

        // 左侧图片不可见文字一定顶头
        if (leftVisible && leftDrawable != null) {
            imgLeft.setImageDrawable(leftDrawable);
            imgLeft.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        } else {
            imgLeft.setVisibility(View.GONE);
        }
    }

    private void initRightImage() {
        LayoutParams imgRightParams = new LayoutParams(DisplayUtils.dip2px(getContext(), IMAGE_SIZE_DP), DisplayUtils.dip2px(getContext(),
                IMAGE_SIZE_DP));
        imgRightParams.addRule(RelativeLayout.CENTER_VERTICAL);
        imgRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        imgRight = new ImageView(getContext());
        imgRight.setLayoutParams(imgRightParams);
        imgRight.setId(R.id.baseline_right_img);

        // 右侧图片不可见文字可能顶尾部也可能需要跟其他视图对齐
        if (rightVisible && rightDrawable != null) {
            imgRight.setImageDrawable(rightDrawable);
            imgRight.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        } else if (!rightVisible) {
            imgRight.setVisibility(View.INVISIBLE);
        }
    }

    private void initLeftText() {
        LayoutParams leftTextParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftTextParams.addRule(RelativeLayout.CENTER_VERTICAL);

        if (leftVisible && leftDrawable != null) {
            leftTextParams.addRule(RelativeLayout.RIGHT_OF, imgLeft.getId());
            leftTextParams.setMargins(DisplayUtils.dip2px(getContext(), BASE_MARGIN_DP), 0, DisplayUtils.dip2px(getContext(), BASE_MARGIN_DP), 0);
        } else {
            leftTextParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        }

        tvLeft = new TextView(getContext());
        tvLeft.setText(leftText);
        tvLeft.setTextColor(leftColor);
        tvLeft.setTextSize(leftSize);
        tvLeft.setSingleLine(true);
        tvLeft.setLayoutParams(leftTextParams);
        tvLeft.setId(R.id.baseline_left_text);
    }

    private void initRightText() {
        LayoutParams rightTextParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         rightTextParams.addRule(RelativeLayout.CENTER_VERTICAL);
        rightTextParams.addRule(RelativeLayout.RIGHT_OF, tvLeft.getId());

        if (rightVisible) {
            //rightTextParams.addRule(RelativeLayout.LEFT_OF, imgRight.getId());
            rightTextParams.setMargins(DisplayUtils.dip2px(getContext(), 50), 0, DisplayUtils.dip2px(getContext(), BASE_MARGIN_DP), 0);
        } else {
            // rightTextParams.addRule(RelativeLayout.LEFT_OF, imgRight.getId());
            rightTextParams.setMargins(DisplayUtils.dip2px(getContext(), 50), 0, DisplayUtils.dip2px(getContext(), BASE_MARGIN_DP), 0);
//            rightTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//            rightTextParams.setMargins(DisplayUtils.dip2px(getContext(), 50), 0, DisplayUtils.dip2px(getContext(), BASE_MARGIN_DP), 0);
        }

        tvRight = new TextView(getContext());
        tvRight.setText(rightText);
        tvRight.setTextColor(rightColor);
        tvRight.setGravity(Gravity.RIGHT);
        tvRight.setSingleLine(true);
        tvRight.setEllipsize(TextUtils.TruncateAt.END);
        tvRight.setTextSize(rightSize);
        tvRight.setLayoutParams(rightTextParams);
        tvRight.setId(R.id.baseline_right_text);
    }

    public void setLeftText(String leftText) {
        this.leftText = leftText;
        tvLeft.setText(leftText);
    }

    public void setLeftTextColor(ColorStateList leftTextColor) {
        this.leftColor = leftTextColor;
        tvLeft.setTextColor(leftTextColor);
    }

    public void setLeftTextSize(int leftSize) {
        this.leftSize = leftSize;
        tvLeft.setTextSize(leftSize);
    }

    public void setRightText(String rightText) {
        this.rightText = rightText;
        tvRight.setText(rightText);
    }

    public String getRightText() {
        return rightText;
    }

    public String getLeftText() {
        return leftText;
    }

    public void setRightTextColor(ColorStateList rightTextColor) {
        this.rightColor = rightTextColor;
        tvRight.setTextColor(rightTextColor);
    }

    public void setRightTextSize(int rightSize) {
        this.rightSize = rightSize;
        tvRight.setTextSize(rightSize);
    }

    public void setImgLeftDrawable(Drawable leftDrawable) {
        if (leftDrawable != null) {
            setImgLeftVisible(View.VISIBLE, leftDrawable);
        } else {
            setImgLeftVisible(View.GONE, null);
        }
    }

    public void setImgLeftResource(int resource) {
        this.leftVisible = true;

        imgLeft.setImageResource(resource);
        initLeftText();
    }

    public void setImgLeftVisible(int visiblilty, Drawable drawable) {
        if (visiblilty == View.VISIBLE && drawable != null) {
            this.leftVisible = true;
            this.leftDrawable = drawable;

            imgLeft.setImageDrawable(drawable);
        } else {
            this.leftVisible = false;
            this.leftDrawable = null;

            imgLeft.setVisibility(View.GONE);
        }
        initLeftText();
    }

    public void setImgRightDrawable(Drawable rightDrawable) {
        setImgRightVisible(View.VISIBLE, rightDrawable);
    }

    public void setImgRightVisible(int visibilty, Drawable drawable) {
        if (visibilty == View.VISIBLE) {
            imgRight.setVisibility(View.VISIBLE);
            imgRight.setImageDrawable(drawable);

            this.rightVisible = true;
            this.rightDrawable = drawable;

        } else {
            imgRight.setVisibility(View.INVISIBLE);
            this.rightVisible = false;
        }
        initRightText();
    }

    public String getRightContent() {
        String str = tvRight.getText().toString();

        return str;
    }

}
